package com.zhengxiang.onlinexamination.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhengxiang.onlinexamination.component.exception.service.ServiceException;
import com.zhengxiang.onlinexamination.dao.SysUserMapper;
import com.zhengxiang.onlinexamination.entity.CustomerUser;
import com.zhengxiang.onlinexamination.dao.CustomerUserDao;
import com.zhengxiang.onlinexamination.entity.ExamPaper;
import com.zhengxiang.onlinexamination.entity.SysDept;
import com.zhengxiang.onlinexamination.entity.SysUser;
import com.zhengxiang.onlinexamination.entity.vo.CustomerUserVo;
import com.zhengxiang.onlinexamination.service.CustomerUserService;
import com.zhengxiang.onlinexamination.service.SysDeptService;
import com.zhengxiang.onlinexamination.service.SysRoleService;
import com.zhengxiang.onlinexamination.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 员工(CustomerUser)表服务实现类
 *
 * @author makejava
 * @since 2023-03-29 22:12:08
 */
@Transactional(rollbackFor = Exception.class)
@Service("customerUserService")
public class CustomerUserServiceImpl extends ServiceImpl<CustomerUserDao,CustomerUser> implements CustomerUserService {
    @Resource
    private CustomerUserDao customerUserDao;

    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public IPage<CustomerUser> pageList(CustomerUserVo customerUserVo) {
        QueryWrapper<CustomerUser>q = new QueryWrapper<>();
        return customerUserDao.selectPage(new Page<>(customerUserVo.getCurrent(),customerUserVo.getSize()),q);
    }

    @Override
    public void addCustomerUser(CustomerUserVo customerUserVo) {
        QueryWrapper<CustomerUser> q= new QueryWrapper<>();
        q.lambda().eq(CustomerUser::getUnionId,customerUserVo.getUnionId());
        CustomerUser customerUser = customerUserDao.selectOne(q);
        if(Objects.nonNull(customerUser)){
            throw new ServiceException("用户已经存在，手机号为："+customerUser.getPhoneNumber());
        }
        customerUserDao.insert(customerUserVo);


    }
}
